package com.hntxk.cqzd.flup.controller;


import com.hntxk.cqzd.common.action.CqzdBaseController;
import com.hntxk.cqzd.flup.entity.FlupFile;
import com.hntxk.cqzd.flup.service.FlupFileService;
import com.hntxk.cqzd.flup.service.impl.FlupFileServiceImpl;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("cqzd/do/flupfile")
@Slf4j
public class FlupFileController extends CqzdBaseController
{


    /** 查询页面的path */

    private final static String LIST_JSP_PATH = "cqzd/flup/flupFile_list";

    private final FlupFileService flupFileService;


    @Autowired
    public FlupFileController(@Qualifier("flupFileManageService") FlupFileService flupFileService) {
        super(flupFileService, LogManager.getLogger(FlupFileController.class));
        this.flupFileService = flupFileService;
    }

    /**
     *
     * @return 患者基本信息主页
     */
    @ApiIgnore
    @GetMapping(value = "/index")
    public String index() {
        return LIST_JSP_PATH;
    }

    /**
     * 查询基础信息
     * @param model
     * @param request
     * @return
     */
    @ApiOperation("查询基础信息")
    @RequestMapping(value = "/queryList",method = {RequestMethod.GET,RequestMethod.POST}, produces= PRODUCE_UTF8_JSON)
    @ResponseBody
    public String queryFlupFile(@ModelAttribute FlupFile model, HttpServletRequest request) {

        try {
            return toJson(model, flupFileService.query2Json(FlupFileServiceImpl.FLUPFILE_MAPPER_NAMESPACE + "query", model, true));
        } catch (Exception e) {
            return getError(e);
        }
    }

    @ApiImplicitParam(name = "fileCode", value = "文件Code",example = "abcde",required = true, dataTypeClass = String.class)
    @GetMapping(value = "getFileByCode/{fileCode}",produces = PRODUCE_UTF8_JSON)
    @ResponseBody
    public ResponseEntity<byte[]> getFile(@PathVariable("fileCode") String fileCode) {
        return flupFileService.getFile(fileCode);
    }

    @RequestMapping(value = "getFileByFkId/{id}",produces = PRODUCE_UTF8_JSON)
    @ResponseBody
    public String getFileByFkId(@PathVariable String id){
        Long flupId=Long.parseLong(id);
        return toJson(flupFileService.getByFkId(flupId));
    }

    /**
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "getByDiacrisisId/{id}",produces = PRODUCE_UTF8_JSON)
    @ResponseBody
    public String getByDiacrisisId(@PathVariable String id){
        Long diacrisisId=Long.parseLong(id);
        return toJson(flupFileService.getByDiacrisisId(diacrisisId));
    }


}
